package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;


    // 部门新增或修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value={"department:saveOrUpdate","部门新增或修改"},logical= Logical.OR)
    public JsonResult saveOrUpdate(Department department){
        try{
            if(department.getId() != null){
                departmentService.update(department);
            }else {
                departmentService.save(department);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
        return new JsonResult(true,"操作成功!!!"); // localhost/department/list.do
    }

    // 查询所有部门数据
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value={"department:delete","部门删除"},logical= Logical.OR)
    public JsonResult delete(Long id){
        try{
            departmentService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }
        return new JsonResult(true,"删除成功"); // localhost/department/list.do
    }

    // 查询所有部门数据
    @RequestMapping("/list")
    @RequiresPermissions(value={"department:list","部门列表"},logical= Logical.OR)
    public String list(Model model, @ModelAttribute("qo")QueryObject qo){
        PageInfo query = departmentService.query(qo);
        model.addAttribute("pageResult", query);
        return "department/list";
    }
}
